package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.exception.CustomException;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApArticleServiceImpl implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    /**
     * @param dto  浏览器传过来的参数
     * @param type 查询类型  1：查询更多 2：查询更新
     * @return 返回文章的集合
     */
    @Override
    public ResponseResult loadArticleList(ArticleHomeDto dto, byte type) {
        List<ApArticle> apArticleList = apArticleMapper.loadArticleList(dto, type);
        return ResponseResult.okResult(apArticleList);
    }

    /**
     * 保存app端相关文章
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        // 1.检查参数是否为空
        if (null == dto) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2.拷贝数据到pojo
        ApArticle article = new ArticleDto();
        BeanUtils.copyProperties(dto,article);
        // 3.判断传递过来的参数中是否包含了id 如果包含了则更新 如果没有则新增
        if(null == article.getId()){
            // 3.1 保存文章数据
            apArticleMapper.insert(article);

            // 3.2 保存文章配置
            ApArticleConfig config = new ApArticleConfig(article.getId());
            apArticleConfigMapper.insert(config);

            // 3.3 保存文章内容
            ApArticleContent content = new ApArticleContent();
            content.setArticleId(article.getId());
            content.setContent(dto.getContent());
            apArticleContentMapper.insert(content);
        }else {
            // 更新文章数据
            apArticleMapper.updateById(article);

            // 查询原有数据
            ApArticleContent apArticleContent = apArticleContentMapper
                    .selectOne(Wrappers.<ApArticleContent>lambdaQuery()
                            .eq(ApArticleContent::getArticleId, dto.getId()));

            // 对原有数据进行覆盖操作
            apArticleContent.setContent(dto.getContent());
            // 更新内容
            apArticleContentMapper.updateById(apArticleContent);
        }

        return ResponseResult.okResult(article.getId());
    }
}
